package com.soft.javase.day06.string;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/4/24
 */
public class StringDemo1 {
    public static void main(String[] args) {

        // 使用 + 操作符拼接字符串
        long startTime1 = System.currentTimeMillis();
        String str = "";
        for (int i = 0; i < 50000; i++) {
            str += "a";
        }
        long endTime1 = System.currentTimeMillis();
        long duration1 = endTime1 - startTime1;

        // 使用StringBuilder拼接字符串
        long startTime2 = System.currentTimeMillis();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 50000; i++) {
            sb.append("a");
        }
        long endTime2 = System.currentTimeMillis();
        long duration2 = endTime2 - startTime2;

        System.out.println("使用 + 操作符拼接耗时: " + duration1 + " 毫秒");
        System.out.println("使用StringBuilder拼接耗时: " + duration2 + " 毫秒");
    }
}
